package ${packageName}.controller;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ${packageName}.dto.request.${entity}PagerRequest;

import ${packageName}.service.${entity}Service;

@Controller
@RequestMapping(value = "/admin/${entityLowerCase}")
public class ${entity}Controller extends BaseController {

	@Autowired
	private ${entity}Service ${entityLowerCase}Service;


	@RequestMapping(value = {"/list"}, method = RequestMethod.GET)
	public String list(${entity}PagerRequest request, Model model) {
		model.addAttribute("page", ${entityLowerCase}Service.find(request));
		model.addAttribute("${entityLowerCase}", request);
		return viewName("list");
	}

    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String showCreateForm(Model model) {
        model.addAttribute(Constants.OP_NAME, "新增");
        if (!model.containsAttribute("m")) {
		${entity} m = new ${entity}();
        model.addAttribute("m", m);
        }
        return viewName("editForm");
	}

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public String create(
        Model model, @Valid @ModelAttribute("m") ${entity} m, BindingResult result,
        RedirectAttributes redirectAttributes) {
        if (hasError(result)) {
        return showCreateForm(model);
        }
		${entityLowerCase}Service.add(m);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "新增成功");
        return redirectToUrl("list");
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.GET)
	public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute(Constants.OP_NAME, "修改");
        model.addAttribute("m", ${entityLowerCase}Service.get(id));
        return viewName("editForm");
	}

	@RequestMapping(value = "{id}/update", method = RequestMethod.POST)
	public String update(
        Model model, @Valid @ModelAttribute("m") ${entity} m, BindingResult result,
        RedirectAttributes redirectAttributes) {
        if (hasError(result)) {
        return showUpdateForm(m.getId(), model);
        }
		${entityLowerCase}Service.update(m);
        redirectAttributes.addFlashAttribute(Constants.MESSAGE, "修改成功");
        return redirectToUrl("list");
	}
}
